package com.zrx.govern.component.easyexcel.starter.entity;

import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author zhangxiaowei236
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class HttpResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    @Getter
    @Setter
    private Integer status;
    @Getter
    @Setter
    private String message;
    @Getter
    @Setter
    private String error;
    @Getter
    @Setter
    private String path;
    @Getter
    @Setter
    private String timestamp;
    @Getter
    @Setter
    private T data;

    public static <T> HttpResult<T> ok() {
        return restResult(null, HttpResultConstants.SUCCESS, "success");
    }

    public static <T> HttpResult<T> ok(T data) {
        return restResult(data, HttpResultConstants.SUCCESS, "success");
    }

    public static <T> HttpResult<T> ok(T data, String message) {
        return restResult(data, HttpResultConstants.SUCCESS, message);
    }

    public static <T> HttpResult<T> failed() {
        return restResult(null, HttpResultConstants.FAIL, "fail");
    }

    public static <T> HttpResult<T> failed(String message) {
        return restResult(null, HttpResultConstants.FAIL, message);
    }

    public static <T> HttpResult<T> failed(T data) {
        return restResult(data, HttpResultConstants.FAIL, "fail");
    }

    public static <T> HttpResult<T> failed(T data, String message) {
        return restResult(data, HttpResultConstants.FAIL, message);
    }

    public static <T> HttpResult<T> failed(Integer status, T data, String message) {
        return restResult(data, status, message);
    }

    public static <T> HttpResult<T> exception(Integer status, String message) {
        return restResult(null, status, message);
    }

    private static <T> HttpResult<T> restResult(T data, Integer status, String message) {
        HttpResult<T> apiResult = new HttpResult();
        apiResult.setStatus(status);
        apiResult.setData(data);
        apiResult.setMessage(message);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        apiResult.setTimestamp(LocalDateTime.now().format(dtf));
        return apiResult;
    }


}
